package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.LargeAppointme;
import com.toommi.refuse.entity.Ordertableinfo;
import com.toommi.refuse.exception.AppUserException;
import com.toommi.refuse.exception.CommonStatusException;
import com.toommi.refuse.exception.RefuseTypeException;
import com.toommi.refuse.exception.WechatUserException;
import com.toommi.refuse.service.LargeAppointmeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ..
 * 大件回收预约服务
 *
 * @author yangg
 * @since 2019-07-31 09:30
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping(value = "")
@PermissionController(groupCode = "admin_large_appoint", groupName = "大件回收预约管理")
@Api(tags = "大件回收预约")
public class LargeAppointmeController {

    private LargeAppointmeService appointmeService;

    /**
     * 新增大件回收预约请求
     *

     * @return 操作结果
     */
    @ApiOperation(value = "新增大件回收预约请求", notes = "新增大件回收预约请求")
    @ApiImplicitParam(name = "appointme", value = "大件回收预约请求详情实体", required = true, dataType = "LargeAppointme")
    @PostMapping(value = "/api/large/appointme", produces = "application/json;charset=UTF-8")
    public CommonResponse addAppoint(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            appointmeService.addAppointme(request);

        } catch (WechatUserException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 获取订单列表
     *

     * @return 操作结果
     */
    @ApiOperation(value = "获取订单列表", notes = "获取订单列表")

    @GetMapping(value = "/api/large/orderlist")
    public CommonResponse orderlist(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.orderlist(request));

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 获取订单详情
     *

     * @return 操作结果
     */
    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")

    @GetMapping(value = "/api/large/orderinfo")
    public CommonResponse orderinfo(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.orderinfo(request));

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }



    /**
     * 获取回收端用户信息
     *
     * @return 操作结果
     */
    @ApiOperation(value = "获取回收端用户信息", notes = "获取回收端用户信息")

    @GetMapping(value = "/api/large/getappinfo")
    public CommonResponse getappinfo(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.getappinfo(request));

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
    /**
     * 获取用户端用户信息
     *

     * @return 操作结果
     */
    @ApiOperation(value = "获取用户端用户信息", notes = "获取用户端用户信息")
    @GetMapping(value = "/api/large/getuserinfo")
    public CommonResponse getuserinfo(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.getuserinfo(request));

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 修改用户信息
     *

     * @return 操作结果
     */
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")

    @PostMapping(value = "/api/large/updateuserinfo")
    public CommonResponse updateuserinfo(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            appointmeService.updateuserinfo(request);

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
    /**
     * 结算
     *

     * @return 操作结果
     */
    @ApiOperation(value = "结算", notes = "结算")

    @PostMapping (value = "/api/large/Settlement")
    public CommonResponse Settlement(@RequestBody Ordertableinfo ordertableinfo) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
         appointmeService.Settlement(ordertableinfo);

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 更新状态
     *

     * @return 操作结果
     */
    @ApiOperation(value = "更新状态", notes = "更新状态")

    @PostMapping (value = "/api/large/updatestatus")
    public CommonResponse updatestatus(String id,String status) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            appointmeService.updatestatus(id,status);

        }  catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除大件回收预约请求
     *
     * @param id 预约请求id
     * @return 操作结果
     */
    @ApiOperation(value = "删除大件回收预约请求", notes = "删除大件回收预约请求")
    @ApiImplicitParam(name = "id", value = "大件回收预约请求id", required = true, dataType = "Long")
    @RequiresPermissions("admin:large:appointme:delete")
    @PermissionReport(value = "admin:large:appointme:delete", name = "删除预约请求", group = "admin_large_appoint")
    @DeleteMapping(value = "/admin/large/appointme/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse deleteAppoint(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            appointmeService.deleteAppointme(id);
        } catch (Exception e) {
            log.error("delete large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 测试
     *
     * @param id 预约请求id
     * @return 操作结果
     */
    @DeleteMapping(value = "/api/large/test/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse deleteAppointTest(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.deleteAppointTest(id));
        } catch (Exception e) {
            log.error("delete large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 移动端更新预约请求信息--更新状态
     *
     * @param appointme 预约请求信息
     * @return 更新结果
     */
    @ApiOperation(value = "移动端更新大件回收预约请求状态", notes = "更新大件回收预约请求")
    @ApiImplicitParam(name = "appointme", value = "大件回收预约请求详情实体", required = true, dataType = "LargeAppointme")
    @PutMapping(value = "/api/large/appoint", produces = "application/json;charset=UTF-8")
    public CommonResponse updateAppointme(@RequestBody LargeAppointme appointme) {

        CommonResponse commonResponse = CommonResponse.success();
        //避免虚假请求构造，App端只允许更新状态 不允许更新责任人
        appointme.setAppUserId(-1L);
        return getCommonResponse(appointme, commonResponse);
    }


    /**
     * 管理端更新预约请求--更新状态&分派人
     *
     * @param appointme 预约请求
     * @return 操作结果
     */
    @ApiOperation(value = "管理端更新大件回收预约请求状态及分派人", notes = "新增大件回收预约请求")
    @ApiImplicitParam(name = "appointme", value = "大件回收预约请求详情实体", required = true, dataType = "LargeAppointme")
    @RequiresPermissions("admin:large:appoint:update")
    @PermissionReport(value = "admin:large:appoint:update", name = "编辑预约请求", group = "admin_large_appoint")
    @PutMapping(value = "/admin/large/appoint", produces = "application/json;charset=UTF-8")
    public CommonResponse modifyAppointme(@RequestBody LargeAppointme appointme) {

        CommonResponse commonResponse = CommonResponse.success();
        return getCommonResponse(appointme, commonResponse);

    }

    /**
     * 查询预约请求列表
     *
     * @param pageNum         页码
     * @param pageSize        每页数据量
     * @param wechatUserId    预约申请人id
     * @param appUserId       分派人id
     * @param status          状态
     * @param districtId      行政区id
     * @param streetId        街道id
     * @param neighbourhoodId 小区id
     * @param startTime       开始时间
     * @param endTime         截至时间
     * @return 预约请求列表
     */
    @ApiOperation(value = "新增大件回收预约请求", notes = "新增大件回收预约请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "wechatUserId", value = "微信用户id", required = false, dataType = "int"),
            @ApiImplicitParam(name = "appUserId", value = "分派人id", required = false, dataType = "int"),
            @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "int"),
            @ApiImplicitParam(name = "districtId", value = "行政区划id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "streetId", value = "街道id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "neighbourhoodId", value = "小区id", required = false, dataType = "long"),
            @ApiImplicitParam(name = "startTime", value = "查询开始时间", required = true, dataType = "long"),
            @ApiImplicitParam(name = "endTime", value = "查询结束时间", required = true, dataType = "long")
    })
    @GetMapping(value = "/api/large/appointmes", produces = "application/json;charset=UTF-8")
    public CommonResponse findAppointmes(@RequestParam int pageNum, @RequestParam int pageSize,
                                         @RequestParam(required = false, defaultValue = "-1") int wechatUserId,
                                         @RequestParam(required = false, defaultValue = "-1") int appUserId,
                                         @RequestParam(required = false, defaultValue = "-1") int status,
                                         @RequestParam(required = false) String districtId,
                                         @RequestParam(required = false) String streetId,
                                         @RequestParam(required = false, defaultValue = "-1") long neighbourhoodId,
                                         @RequestParam(required = false, defaultValue = "-1") long startTime,
                                         @RequestParam(required = false, defaultValue = "-1") long endTime
    ) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.findAppoints(pageNum, pageSize, wechatUserId, appUserId, status,
                    districtId, streetId, neighbourhoodId, startTime, endTime));
        } catch (Exception e) {
            log.error("find large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 获取预约详情信息
     *
     * @param id 预约详情id
     * @return 预约详情信息
     */
    @ApiOperation(value = "获取预约信息详情", notes = "获取预约信息详情")
    @ApiImplicitParam(name = "id", value = "预约请求id", required = true, dataType = "long")
    @GetMapping(value = "/api/large/appointme/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse findAppointmeDetail(@PathVariable(name = "id") long id) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(appointmeService.findDetail(id));
        } catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("get large appointme detail error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;

    }

    /**
     * 更新预约请求
     *
     * @param appointme      预约请求
     * @param commonResponse 响应对象
     * @return 操作结果
     */
    private CommonResponse getCommonResponse(@RequestBody LargeAppointme appointme, CommonResponse commonResponse) {
        try {
            appointmeService.updateAppointme(appointme);
        } catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (AppUserException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (CommonStatusException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("app update large appointme error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 功能描述: 回收端修改订单废品类型
     * @param: []
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/17 0017 下午 14:50
     */
    @PostMapping("/api/large/updatetype")
    public CommonResponse updateOrderRecycleType(String id, String type) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            appointmeService.updateTypeContentById(id,type);
        } catch (RefuseTypeException e) {
            log.error("refuse type error:{}",e);
            commonResponse = CommonResponse.serverError();
        } catch (Exception e) {
            log.error("app update large appointeme typecontent error:{}",e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 功能描述: 获取上一次电话和地址
     * @param: [request]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/18 0018 上午 11:35
     */
    @GetMapping("/api/large/lastphoneaddr")
    public CommonResponse getLastPhoneAndAddr(HttpServletRequest request) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            Map<String, Object> res = appointmeService.getLastPhoneAndAddr(request);
            commonResponse.setData(res);
        } catch (Exception e) {
            log.error("get last deal address error:{}",e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
}
